package com.yww.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yww.entity.Speak;
import com.yww.service.ISpeakService;
import com.yww.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器(公司公告控制层)
 * </p>
 *
 * @author 雯雯yang
 * @since 2020-06-16
 */
@RestController
@RequestMapping("/speak")
public class SpeakController {
    @Autowired
    ISpeakService iSpeakService;

    /**
     * 添加公告
     * @param speak
     * @return
     */
    @RequestMapping("/addSpeak")
    public Object addSpeak(Speak speak){
        speak.setShowStatus(0);    //未读公告
        Date date=new Date();
        speak.setCreateTime(date);//当前时间
        boolean save = iSpeakService.save(speak);
        if(save){
            return new Result(0,"公告添加成功");
        }
        return new Result(1,"公告添加失败");
    }


    /**
     * 修改公告内容
     * @param
     * @return
     */
    @RequestMapping("/updSpeak")
    public Object updSpeak(Integer id,Integer integer,Speak s){
//        s.setId(id);
//        s.setShowStatus(integer);
        Speak speak=new Speak();
          speak.setId(id);
          speak.setShowStatus(integer);
        boolean b = iSpeakService.updateById(speak);
        if(b){
            return new Result(0,"公告修改成功");
        }
        return new Result(1,"公告修改失败");
    }

    @RequestMapping("/updateSpeak")
    public Object updateSpeak(Speak s){
        boolean b = iSpeakService.updateById(s);
        if(b){
            return new Result(0,"公告修改成功");
        }
        return new Result(1,"公告修改失败");
    }

    /**
     * 获取公告列表
     * @return
     */
    @RequestMapping("/getSpeakList")
    public Object getSpeakList(){
        QueryWrapper<Speak> queryWrapper=new QueryWrapper<Speak>();
        List<Speak> list = iSpeakService.list(queryWrapper.eq("show_status", 0).orderByDesc("create_time"));

        return list;

    }


    /**
     * 管理员/经理分页获取多条公告
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getPageSpeakList")
    public Object getPageSpeakList(@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit){
        //分页-需要分页的配置文件
        Page<Speak> p = new Page<>(page, limit);
        Page<Speak> page1 = iSpeakService.page(p);
        List<Speak> records = page1.getRecords();
        int total = (int) page1.getTotal();
        return new Result(0, "查询成功",total,records);

    }



}
